package com.ogawa.projectcommon.activity

import android.net.Uri
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.LogUtils
import com.ogawa.base.base.BaseActivity
import com.ogawa.projectcommon.R
import com.ogawa.projectcommon.constants.UrlPathConstant
import com.tencent.smtt.sdk.WebChromeClient
import com.tencent.smtt.sdk.WebSettings
import com.tencent.smtt.sdk.WebView
import com.tencent.smtt.sdk.WebViewClient
import kotlinx.android.synthetic.main.activity_my_web_view.*

@Route(path = UrlPathConstant.ACTIVITY_WEB_VIEW)
class MyWebViewActivity : BaseActivity() {

    @JvmField
    @Autowired(name = "titleStr")
    var titleStr = ""

    @JvmField
    @Autowired(name = "url")
    var url = ""

    override fun getLayoutId(): Int {
        return R.layout.activity_my_web_view
    }


    override fun initView() {
        super.initView()
        ARouter.getInstance().inject(this)
        setTitle(titleStr)
        initWebView()
        LogUtils.e("加载url：$url")


        mWebView.loadUrl(url)

    }


    fun initWebView() {

//        android.webkit.WebView.setWebContentsDebuggingEnabled(true);
//        WebView.setWebContentsDebuggingEnabled(true);
        val webSetting: WebSettings = mWebView.getSettings()
        webSetting.allowFileAccess = true
        webSetting.layoutAlgorithm = WebSettings.LayoutAlgorithm.NARROW_COLUMNS
        webSetting.setSupportZoom(false)
        webSetting.builtInZoomControls = false
        webSetting.useWideViewPort = true
        webSetting.setSupportMultipleWindows(false)
        webSetting.loadWithOverviewMode = true
        webSetting.setAppCacheEnabled(true)
        webSetting.databaseEnabled = true
        webSetting.domStorageEnabled = true
        webSetting.javaScriptEnabled = true
        webSetting.setGeolocationEnabled(true)
        webSetting.setAppCacheMaxSize(Long.MAX_VALUE)
        webSetting.setAppCachePath(getDir("appcache", 0).path)
        webSetting.databasePath = getDir("databases", 0).path
        webSetting.setGeolocationDatabasePath(
            getDir("geolocation", 0)
                .path
        )
        // webSetting.setPageCacheCapacity(IX5WebSettings.DEFAULT_CACHE_CAPACITY);
        // webSetting.setPageCacheCapacity(IX5WebSettings.DEFAULT_CACHE_CAPACITY);
        webSetting.pluginState = WebSettings.PluginState.ON_DEMAND
        webSetting.mediaPlaybackRequiresUserGesture = false
        webSetting.javaScriptCanOpenWindowsAutomatically = true
        mWebView.setWebChromeClient(object : WebChromeClient() {
            override fun onProgressChanged(webView: WebView, i: Int) {
                super.onProgressChanged(webView, i)
                progressBar.setProgress(i)
                if (i > 90) {
                    LogUtils.e("progressBar.setVisibility(View.GONE)")
                    try {
                        progressBar.setVisibility(View.GONE)
                    } catch (e: Exception) {
                        e.printStackTrace()
                    }
                } else {
                    progressBar.setVisibility(View.VISIBLE)
                }
            }
        })
        mWebView.setWebViewClient(object : WebViewClient() {
            override fun shouldOverrideUrlLoading(
                webView: WebView,
                url: String
            ): Boolean {
                val uri = Uri.parse(url)
                var names: Set<String>? = null
                try {
                    names = uri.queryParameterNames
                } catch (e: Exception) {
                    e.printStackTrace()
                }
                if (names == null) {
//                    ToastUtils.showShort("解析错误：" + url);
                    return false
                }
                return false
            }
        })
    }


}